//
//  TVRefreshGifHeader.m
//  HeFanTV
//
//  Created by bai on 16/8/12.
//  Copyright © 2016年 SAGA. All rights reserved.
//

#import "CBTRefreshGifHeader.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <Masonry/Masonry.h>

NSString * const NotificationEstimateOverRefresh = @"NotificationEstimateOverRefresh";

@interface CBTRefreshGifHeader()

@property (nonatomic, strong) UIImage *loading;
@property (nonatomic, strong) UIImage *pulling;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation CBTRefreshGifHeader

+ (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock
                          backgroundColor:(UIColor *)backgroundColor{
    CBTRefreshGifHeader *header = [CBTRefreshGifHeader headerWithRefreshingBlock:refreshingBlock];
    header.backgroundColor = backgroundColor;
    
    return header;
}

#pragma mark - 重写方法
#pragma mark 基本设置
- (void)prepare
{
    [super prepare];
  
    self.gifView.contentMode = UIViewContentModeScaleAspectFit;
    self.gifView.mj_w = ScreenNarrowSide;  //self.mj_w * 0.5 - 90;
    self.gifView.mj_h = AspectScale(90/2);  //self.mj_w * 0.5 - 90;

    
    self.lastUpdatedTimeLabel.hidden = YES;
    self.stateLabel.hidden = YES;
    
    NSMutableArray *images = [NSMutableArray array];
    
    self.pulling = [UIImage imageNamed:@"headerRefresh.bundle/pulling"];
    self.loading = [UIImage imageNamed:@"headerRefresh.bundle/loading"];
    self.imageView.image = self.pulling;
    
    for(int i = 0; i< 80; i++){
        NSString *name = [NSString stringWithFormat:@"headerRefresh.bundle/%02d", i];
        
        UIImage *image = [UIImage imageNamed:name];
        [images addObject:image];
    }
    // 设置普通状态的动画图片
    [self setImages:[images subarrayWithRange:NSMakeRange(0, 18)] duration:1 forState:MJRefreshStateIdle];
    
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [self setImages:images duration:images.count*0.05  forState:MJRefreshStatePulling];
    
    // 设置正在刷新状态的动画图片
    [self setImages:images duration:images.count*0.05 forState:MJRefreshStateRefreshing];
    MJRefreshState state = self.state;
    self.state = MJRefreshStateIdle;
    self.state = state;
   
}
- (void)setState:(MJRefreshState)state{
    [super setState:state];
    
    if (state == MJRefreshStatePulling
        ||state == MJRefreshStateIdle){
        if (self.pulling){
            self.imageView.image = self.pulling;
        }
    }else{
        if (self.loading){
            self.imageView.image = self.loading;
        }
    }
}
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    
    CGPoint offset = self.scrollView.contentOffset;
    [self.backgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(MIN(0, CGRectGetHeight(self.bounds) + offset.y));
    }];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor{
    [super setBackgroundColor:[UIColor clearColor]];
    
    self.backgroundView.backgroundColor = backgroundColor;
}

- (UIView*)backgroundView{
    if (!_backgroundView){
        _backgroundView = [UIView new];
     //   _backgroundView.backgroundColor = COLOR_BACKGROUND;
        
        [self insertSubview:_backgroundView atIndex:0];
        
        [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return _backgroundView;
}

- (UIImageView*)imageView{
    if (!_imageView){
        _imageView = [UIImageView new];
        //   _backgroundView.backgroundColor = COLOR_BACKGROUND;
        
        [self insertSubview:_imageView aboveSubview:self.backgroundView];
        
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return _imageView;
}

@end
